<?php
loadHelper('form');
loadHelper('url');
loadHelper('language');
include 'model.php';
loadView('header');
$profile = $_POST['profile'];
if(isset($profile) and !empty($profile))
{
    //Tạo rule kiểm tra dữ liệu
    $rules = array(
                    'Fullname' => array('required','max-length-100','min-length-3'),
                    'Email' => array('required', 'max-length-100', 'min-length-10'),
                    'Address' => array('required', 'max-length-100', 'min-length-5')
                );
    $errors = form_validate($rules, $profile);
    if(!empty($errors))
    {
        include 'view_form.php';
        return;
    }
    else
    {
        update_profile($profile, $_SESSION['UID']);
        $messages[] = 'Update sucessfully!';
    }
}

if(isset($_GET['id']))
{
    $profile = get_profile($_GET['id']);
            $num_entries = count(get_entries($_SESSION['UID']));
            $num_votes = get_all_votes($_SESSION['UID']);
    include 'view.php';
}
//Xu ly upload avatar
$new_name =  $_SESSION['UID'] . '.' . @end(explode('.', $_FILES['avatar']['name']));
if(isset($_FILES['avatar']) and $_FILES['avatar']['name'] != '')
{
    if(move_uploaded_file($_FILES['avatar']['tmp_name'], BASE_DIR . '\data\avatars\\' . $new_name) === true)
    {
        update_profile(array('Avatar' => $new_name), $_SESSION["UID"]);
    }
    else
    {
        $error[] = 'Error upload avatar';        
    }
}
loadView('footer');
?>
